// 一 、Array.from()

let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
};
console.log(arrayLike);

// ES5 
// slice 从已有数组返回选定的元素
// 不会修改数组
var arr1 = [].slice.call(arrayLike);
console.log(arr1);

// ES6 

let arr2 = Array.from(arrayLike);
console.log(arr2);

// DOM 
// NodeList
let ps = document.querySelectorAll('p');
Array.from(ps).forEach(function(p) {
    console.log(p);
});

// arguments 

function foo(a, b) {
    var args = Array.from(arguments);
    console.log(args);
    return a + b;
}
foo(1, 2);

//
let hello = Array.from('hello');
console.log(hello);

// set
let namesSet = new Set(['a', 'b']);
let namesSetArray = Array.from(namesSet);
console.log(namesSetArray);

// ======
const toArray = (() =>
    Array.from ? Array.from : obj => [].slice.call(obj)

)();

console.log('================');
// Array.from 接收第二个参数
var arrayfrom1 = Array.from(arrayLike, x => x * x);
// console.log(arrayfrom1);
// 等同于
var arrayfrom2 = Array.from(arrayLike).map(x => x * x);
// console.log(arrayfrom2);

var arr = Array.from([1, 2, 3], (x) => x * x);
console.log(arr); // 1 4 9

let spans = document.querySelectorAll('span.name');
// map()
let names1 = Array.prototype.map.call(spans,s => s.textContent);
console.log(names1); // 获取每个span文本

// Array.from()
let names2 = Array.from(spans,s => s.textContent);
console.log(names2); // 获取每个span文本

let flagTo0 = Array.from([1,,2,,3],(n) => n||0);
console.log(flagTo0);

// 返回各种数据类型
function typesOf(){
	return Array.from(arguments, value => typeof value);
}

let types = typesOf(null,[],NaN);
console.log(types);

// 第一个参数指定第二个参数运行次数
let lens = Array.from({length:2},()=> 'jack');
console.log(lens);

// 返回字符串长度
function countSymbols(string){
	return Array.from(string).length;
}
var strLen = countSymbols('123456');
console.log(strLen);

















